package com.bootdo.order.controller;

import com.bootdo.common.annotation.IpFilter;
import com.bootdo.common.annotation.Log;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.order.domain.ReceiptDO;
import com.bootdo.order.service.ReceiptService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Desc 回单管理
 * @date 2019/12/16
 *
 */
@Controller
@RequestMapping("/orders/receipt")
public class ReceiptController {

    private final String PREFIX = "orders/receipt";

    @Autowired
    private ReceiptService receiptService;

    @GetMapping()
    @RequiresPermissions("orders:receipt:receipt")
    String receipt(){
        return PREFIX + "/list";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("orders:receipt:receipt")
    public PageUtils list(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);
        List<ReceiptDO> list = receiptService.list(query);
        int total = receiptService.count(query);
        PageUtils pageUtils = new PageUtils(list, total);
        return pageUtils;
    }

    @Log("查看凭证")
    @GetMapping("/showImg/{id}")
    String showImg(Model model, @PathVariable("id") Long id){
        ReceiptDO receipt = receiptService.get(id);
        model.addAttribute("receipt", receipt);
        return PREFIX + "/showImg";
    }

    @RequiresPermissions("orders:receipt:edit")
    @Log("审核回款申请")
    @GetMapping("/edit/{id}")
    String edit(Model model, @PathVariable("id") Long id) {
        model.addAttribute("id", id);
        return PREFIX + "/edit";
    }

    @RequiresPermissions("orders:receipt:edit")
    @Log("审核回款申请")
    @PostMapping("/update")
    @ResponseBody
    @IpFilter
    R update(ReceiptDO receipt) {
        int result = receiptService.update(receipt);
        if (result > 0) {
            return R.ok();
        } else if (result == -2) {
            return R.error("当前回单已审核，请勿重复审核");
        }
        return R.error();
    }

}
